<template>
  <div class="body">
    <el-card>
      <img
        class="logo"
        src="http://likede2-admin.itheima.net/img/logo.595745bd.png"
        alt=""
      />

      <el-form :model="from" :rules="rules" ref="login">
        <el-form-item prop="loginName">
          <span class="el-icon-mobile-phone"></span>
          <el-input
            placeholder="请输入用户名"
            type="text"
            v-model="from.loginName"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <span class="el-icon-lock"></span>
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="from.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <span class="el-icon-guide"></span>
          <el-input
            placeholder="请输入验证码"
            type="text"
            v-model="from.code"
          ></el-input>
          <!-- 点击时 count++ 触发 computed 以达到重新赋随机值 -->
          <div @click="count > 20 ? count-- : count++">
            <img
              :src="
                'http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/' +
                captcha
              "
            />
          </div>
        </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      from: {
        loginName: 'admin',
        password: 'admin',
        loginType: 0,
        code: ''
      },
      rules: {
        loginName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      count: 20
    }
  },
  computed: {
    captcha () {
      // 当count改变重新计算随机数
      const arr = []
      for (let i = 48; i <= 57; i++) {
        arr.push(String.fromCharCode(i))
      }
      for (let i = 97; i <= 122; i++) {
        arr.push(String.fromCharCode(i))
      }
      for (let i = 65; i <= 90; i++) {
        arr.push(String.fromCharCode(i))
      }
      const strArr = []
      for (let i = 0; i < this.count; i++) {
        strArr.push(arr[Math.floor(Math.random() * 62)])
      }
      return strArr.join('')
    }
  },
  methods: {
    async login () {
      // 表单非空验证
      this.$refs.login.validate(async (valid) => {
        if (valid) {
          // 将获取图片的随机值放入请求数据的对象中
          this.from.clientToken = this.captcha
          try {
            await this.$store.dispatch('user/loginAction', this.from)
            this.$router.push('/')
            this.$message.success('登录成功')
          } catch (error) {
            // 登录失败清空并跟新验证码
            this.$message.error(error)
            this.count++
            this.from.code = ''
          }
        } else {
          this.$message.error('请完善表单')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  min-height: 100vh;
  background: url(http://likede2-admin.itheima.net/img/background.be4fae7d.png)
    no-repeat center/cover;

  .el-card {
    position: relative;
    width: 518px;
    height: 388px;
    padding: 76px 35px 0;
    border-radius: 10px;
    overflow: visible;
    .logo {
      position: absolute;
      top: 0;
      left: 50%;
      width: 96px;
      height: 96px;
      transform: translate(-50%, -50%);
    }
    ::v-deep .el-card__header {
      background-color: rgba(114, 124, 245, 1);
      text-align: center;
      color: white;
      height: 80px;
      font-size: 18px;
      line-height: 43px;
    }
    ::v-deep .el-card__body {
      padding: 0;
    }
    .el-form-item {
      position: relative;
      width: 446px;
      height: 52px;
      border-radius: 5px;
      border: 1px solid #ccc;
      span {
        float: left;
        line-height: 50px;
        margin-left: 20px;
      }
      .el-input {
        width: 379px;
        height: 50px;
        ::v-deep input {
          height: 100%;
          border: 0;
        }
      }
      div {
        position: absolute;
        bottom: 0;
        right: 0;
        width: 130px;
        height: 50px;
      }
    }
    .el-button {
      width: 446px;
      height: 52px;
      border-radius: 5px;
      background: linear-gradient(262deg, #2e50e1, #6878f0);
    }
    .tc {
      text-align: center;
    }
  }
}
</style>
